<template>
  <div>
    <div class="provider-list">
      <div class="header">
        <div class="header-line1 flex-s">
          <el-button type="primary" @click="onOperation({}, 'add')"
            ><el-icon style="margin-right: 2px"><Plus /></el-icon
            >新建项目</el-button
          >
          <el-button @click="state.showUpload = true"
            ><el-icon style="margin-right: 2px"><Download /></el-icon
            >导入项目</el-button
          >
          <el-button
            v-if="!state.isSuccess"
            type="danger"
            @click="downloadFailFile"
            >下载失败数据</el-button
          >
          <el-button @click="downloadTemplate">下载模板</el-button>
        </div>
        <div class="header-line2 flex-s">
          <el-input
            placeholder="请输入内容"
            v-model="state.params.xiangmumingcheng"
            style="width: 350px"
            size="large"
          >
            <template #append>
              <el-button
                @click="GetList"
                style="min-width: 50px"
                icon="Search"
              />
            </template>
          </el-input>
          <el-select
            width="150"
            show-border
            class="ml20"
            v-model="state.params.xiangmuleixing"
            size="large"
            placeholder="类型"
            clearable
            @change="GetList"
          >
            <el-option
              v-for="item in state.options"
              :key="item.dicValue"
              :label="item.dicLabel"
              :value="item.dicValue"
            >
              {{ item.dicLabel }}
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="content providerTable">
        <el-table
          ref="multipleTableRef"
          border
          :data="state.list"
          style="width: calc(100% - 20px)"
        >
          <el-table-column
            property="xmNo"
            label="编号"
            width="170"
            fixed
            align="center"
          />
          <el-table-column
            property="xiangmumingcheng"
            label="项目名称"
            width="200"
            align="center"
          />
          <el-table-column
            property="chengshi"
            label="城市（省市区）"
            width="200"
            align="center"
          />
          <el-table-column
            property="xiangmuleixing"
            label="类型"
            width="130"
            align="center"
          />
          <el-table-column
            property="yewufenlei1"
            label="一级业务分类"
            width="200"
            align="center"
          />
          <el-table-column
            property="yewufenlei2"
            label="二级业务分类"
            width="200"
            align="center"
          />
          <el-table-column
            property="yewufenlei3"
            label="三级业务分类"
            width="200"
            align="center"
          />
          <el-table-column
            property="xiangmudizhi"
            label="项目地址"
            width="250"
            align="center"
          />
          <el-table-column
            property="zhuangtai"
            label="项目状态"
            width="150"
            align="center"
          />
          <el-table-column
            property="yunyingshang"
            label="项目运营商"
            width="150"
            align="center"
          />
          <el-table-column
            property="xiangmufuzeren"
            label="技术负责人"
            width="160"
            align="center"
          />
          <el-table-column
            property="gongyingshangshuliang"
            label="参与供应商数量"
            width="130"
            align="center"
          />
          <el-table-column
            property="chanpinshuliang"
            label="使用产品数量"
            width="130"
            align="center"
          />
          <el-table-column
            property="xiangmuzhuanjia"
            label="项目专家"
            width="200"
            align="center"
          />
          <el-table-column
            property="beizhu"
            label="备注"
            width="700"
            align="center"
          />
          <el-table-column
            property="createTime"
            label="创建时间"
            width="200"
            align="center"
          />
          <el-table-column
            property="approvalStatus"
            label="审核状态"
            width="100"
            align="center"
          >
            <template #default="scope">
              <span v-if="scope.row.shenhe">{{
                scope.row.shenhe.code == 0
                  ? "待审核"
                  : scope.row.shenhe.code == 1
                  ? "审核通过"
                  : "审核未通过"
              }}</span>
            </template></el-table-column
          >
          <el-table-column
            fixed="right"
            label="操作"
            width="300"
            align="center"
          >
            <template #default="scope">
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'detail')"
                >详情</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'edit')"
                >编辑</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                v-if="scope.row?.shenhe?.code == 0 && state.check === '1'"
                @click.prevent="onOperation(scope.row, 'pass')"
                >通过</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                v-if="scope.row?.shenhe?.code == 0 && state.check === '1'"
                @click.prevent="onOperation(scope.row, 'deny')"
                >不通过</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'note')"
                >备注</el-button
              >
              <el-button
                style="min-width: auto"
                link
                type="primary"
                size="small"
                @click.prevent="onOperation(scope.row, 'delete')"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <h-page
          v-if="state.total"
          v-model:curr-page="state.params.current"
          :size="state.params.size"
          :total="state.total"
          @change="GetList"
        />
      </div>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="40%"
        v-model="state.show"
        center
        style="padding: 0 30px"
      >
        <el-form :model="state.form1">
          <el-form-item label="备注：">
            <el-input
              v-model="state.form1.beizhu"
              :maxlength="500"
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-form-item class="note-button-group">
          <el-button size="large" @click="state.show = false">取消</el-button>
          <el-button
            class="ml50"
            size="large"
            type="primary"
            @click="submitForm"
          >
            提交
          </el-button>
        </el-form-item>
      </el-dialog>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="40%"
        v-model="state.showReview"
        center
        style="padding: 0 30px"
      >
        <el-form :model="state.reviewForm">
          <el-form-item label="不通过原因：">
            <el-input
              v-model="state.reviewForm.shenheBeizhu"
              :maxlength="150"
              type="textarea"
              :rows="8"
            ></el-input>
          </el-form-item>
        </el-form>
        <el-form-item class="note-button-group">
          <el-button size="large" @click="state.showReview = false"
            >取消</el-button
          >
          <el-button class="ml50" size="large" type="primary" @click="review">
            提交
          </el-button>
        </el-form-item>
      </el-dialog>
      <el-dialog
        class="list-dialog"
        show-close="false"
        width="35%"
        v-model="state.showUpload"
        center
        style="padding: 0 30px"
      >
        <p
          style="
            margin-top: -30px;
            margin-bottom: 30px;
            font-weight: bold;
            font-size: 20px;
            color: #333;
          "
        >
          导入项目
        </p>
        <el-form>
          <el-form-item label="上传附件：">
            <bjzOssXlsx
              v-if="state.showUpload"
              uploadType="project"
              @cancel="cancel"
              @success="success"
            ></bjzOssXlsx>
          </el-form-item>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import { reactive, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import {
  GetProjectList,
  NoteProject,
  DeleteProject,
  ReviewProject,
} from "@/api/project";
import { GetDictionary, downloadTFile } from "@/api/system";
import { baseMsg, elMessageTip } from "@/utils/utils";
import bjzOssXlsx from "@/components/bjz-oss-xlsx.vue";

const route = useRoute();
const router = useRouter();
const state = reactive({
  params: {
    current: Number(route.query.page) || 1,
    size: Number(route.query.size) || 10,
    xiangmumingcheng: "",
    xiangmuleixing: "",
  },
  form1: {
    id: "",
    beizhu: "",
  },
  list: [],
  show: false,
  options: [],
  total: 0,
  // 审核表单
  reviewForm: {
    id: null,
    shenheBeizhu: "",
    shenhe: null,
  },
  showReview: false,
  // 是否显示上传文件弹窗
  showUpload: false,
  // 上传的数据成功状态
  isSuccess: true,
  // 失败数据文件下载地址
  failFilePath: "",
  // 失败数据文件名
  failFileName: "",
  check: localStorage.getItem("check"),
});
// 查询项目列表
const GetList = async () => {
  const res = await GetProjectList(state.params);
  if (res.code == 4201) {
    state.list = res.data.records;
    state.total = res.data.total;
  }
};
// 备注提交
const submitForm = async () => {
  const res = await NoteProject(state.form1);
  if (res.code == 4202) {
    baseMsg("备注成功");
    GetList();
  }
  state.show = false;
};
// 查询项目分类
const GetSelect = async () => {
  const res = await GetDictionary({ dicId: "xiangmuleixing" });
  if (res.code == 4201) {
    state.options = res.data;
  }
};
// 下载文件方法
const downloadFile = (url, fileName) => {
  const x = new window.XMLHttpRequest();
  x.open("GET", url, true);
  x.responseType = "blob";
  x.onload = () => {
    const url = window.URL.createObjectURL(x.response);
    const a = document.createElement("a");
    console.log(url);
    a.href = url;
    a.download = fileName;
    a.click();
  };
  x.send();
};
// 下载模板
const downloadTemplate = () => {
  downloadTFile({ type: "xiangmu" }).then((res) => {
    if (res.code == 4201) {
      downloadFile(res.data.filePath, res.data.fileName);
    }
  });
};
// 下载失败文件模板
const downloadFailFile = () => {
  downloadFile(state.failFilePath, state.failFileName);
};
// 取消上传
const cancel = (val) => {
  state.showUpload = val;
};
// 文件上传完成
const success = (data) => {
  state.isSuccess = data.result;
  if (!data.result) {
    baseMsg("部分数据失败,请下载失败文件再重新上传", false);
    state.failFilePath = data.resultFilePath;
    state.failFileName = data.fileName;
  } else {
    baseMsg("上传成功");
  }
  GetList();
  state.showUpload = false;
};
GetList();
GetSelect();
// 操作
const onOperation = (params, operation) => {
  if (operation === "detail") {
    router.push({
      path: `/xiangmu/detail`,
      query: {
        id: params.id,
      },
    });
  }
  if (operation === "note") {
    state.show = true;
    state.form1.id = params.id;
    state.form1.beizhu = params.beizhu;
  }
  if (operation === "delete") {
    elMessageTip({
      ok() {
        DeleteProject({
          id: params.id,
        }).then((res) => {
          if (res.code == 4202) {
            baseMsg("删除成功");
            GetList();
          }
        });
      },
    });
  }
  if (operation === "add" || operation === "edit") {
    router.push({
      path: `/xiangmu/info`,
      query: {
        type: operation,
        id: params.id,
      },
    });
  }
  if (operation === "deny" || operation === "pass") {
    state.showReview = false;
    state.reviewForm.id = params.id;
    state.reviewForm.shenheBeizhu = "";
    state.reviewForm.xmNo = params.xmNo;
    if (operation === "deny") {
      state.showReview = true;
      state.reviewForm.shenhe = -1;
    } else {
      state.reviewForm.shenhe = 1;
      ReviewProject(state.reviewForm).then((res) => {
        if (res.code == 4202) {
          baseMsg("操作成功");
          GetList();
        }
        state.showReview = false;
      });
    }
  }
};
const review = () => {
  if (!state.reviewForm.shenheBeizhu) {
    baseMsg("请输入不通过原因", false);
  } else {
    ReviewProject(state.reviewForm).then((res) => {
      if (res.code == 4202) {
        baseMsg("操作成功");
        GetList();
      }
      state.showReview = false;
    });
  }
};
watch(
  () => route.query,
  () => {
    state.params.current = Number(route.query.page) || 1;
    GetList();
  },
  { deep: true }
);
</script>
<style lang="scss" scoped>
.header {
  border-bottom: 10px solid #f2f2f2;
  border-radius: 10px 10px 0 0;
}
.header-line1 {
  padding: 20px 10px;
  border-bottom: 1px solid #ccc;
  border-radius: 10px 10px 0 0;
}
.header-line2 {
  padding: 20px 10px;
}
.content {
  border: none !important;
  margin: 20px 10px;
  background-color: transparent !important;
}
</style>
<style lang="scss">
.providerTable .el-table__header th {
  background-color: #f2f9fc !important;
}
.note-button-group {
  .el-form-item__content {
    justify-content: center;
  }
}
</style>
